package com.coffeeloversupportlibrary.coffeelover.mercury.game;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Game;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/26 19:18
 * @projectName TurningCoffee
 * @className GameStartActivity.java
 * @description TODO
 */
public class GameStartActivity extends AppCompatActivity {
    private SharedPreferences userInfo;
    private Activity activity = GameStartActivity.this;

    private AppCompatButton btn_plantgn;

    private HttpUtils httpUtils = new HttpUtils();
    private JsonUtils jsonUtils = new JsonUtils();

    private User user;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_activity_no);

        initView();
        setListener();
    }


    void setListener(){
        StartOnClickListener listener = new StartOnClickListener();
        btn_plantgn.setOnClickListener(listener);
    }

    void initView(){
        userInfo = getSharedPreferences("user", 0);
        user = jsonUtils.getUser(userInfo.getString("userInfo", ""));

        btn_plantgn = findViewById(R.id.btn_plantgn);
    }

    void getTree(String userID){
        Call call = httpUtils.getCall("/tree/tree/"+userID);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                Log.e("GetTree", "onResponse: "+res);
                Game game = jsonUtils.getGame(res);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SharedPreferences.Editor editor = userInfo.edit();
                        editor.putString("tree", res);
                        editor.commit();
                    }
                });
            }
        });
    }

    class StartOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_plantgn:
                    String userId = user.getData().getUser().getUserId();
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("userId", userId);
                        jsonObject.put("treeName", "");
                        jsonObject.put("userWater", 0);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String jsonString = jsonObject.toString();
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonString);

                    Call call = httpUtils.getCall(requestBody,"/tree/tree");
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            String res = response.body().string();
                            Log.e("Create", "onResponse: "+res);
                            Game game = jsonUtils.getGame(res);
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(game.getMessage().equals("创建树成功")){
                                        getTree(userId);
                                        Intent intent = new Intent(activity, FirstStageActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            });
                        }
                    });

                    break;
            }
        }
    }
}
